﻿using EnvDTE;
using System;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.UI;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.VisualStudio
{
    public class Ide
    {
        private Package package;


        public Solution Solution
        {
            get
            {
                var dte = (DTE)((IServiceProvider)package).GetService(typeof(DTE));
                return new Solution(dte.Solution);
            }
        }


        public Ide(Package package)
        {
            this.package = package;
        }


        public void ShowErrorListPane()
        {
            var errorList = (IVsErrorList)((IServiceProvider)package).GetService(typeof(SVsErrorList));
            ErrorHandler.ThrowOnFailure(errorList.BringToFront());
        }

        public T FindToolWindowPane<T>()
            where T : ToolWindowPane
        {
            var window = package.FindToolWindow(typeof(T), 0, true);
            if ((null == window) || (null == window.Frame))
            {
                throw new NotSupportedException("Failed to show the code analysis result window pane.");
            }
            return (T)window;
        }

        public T ShowToolWindowPane<T>()
            where T : ToolWindowPane
        {
            var window = FindToolWindowPane<T>();
            var frame = (IVsWindowFrame)window.Frame;
            ErrorHandler.ThrowOnFailure(frame.Show());
            return (T)window;
        }
    }
}
